package Adapt;
//双向适配器模式
//目标接口target
interface TwoWayTarget{
    public void request();
}
//适配者接口
interface TwoWayAdaptee{
    public void specificRequest();
}
//目标实现
class TargetRealize implements TwoWayTarget{

    @Override
    public void request() {
        System.out.println("目标代码被调用");
    }
}
//适配者实现
class AdapteeRealize implements TwoWayAdaptee{

    @Override
    public void specificRequest() {
        System.out.println("适配者代码被调用");
    }
}
//双向适配器
class TwoWayAdapter implements TwoWayAdaptee,TwoWayTarget{
    //双向适配器：创建对象
    private TwoWayTarget twoWayTarget;
    private TwoWayAdaptee twoWayAdaptee;
    //提供双向适配的构造函数
    public TwoWayAdapter(TwoWayTarget twoWayTarget){
        this.twoWayTarget = twoWayTarget;
    }
    public TwoWayAdapter(TwoWayAdaptee twoWayAdaptee){
        this.twoWayAdaptee = twoWayAdaptee;
    }
    @Override
    public void request() {
        twoWayAdaptee.specificRequest();
    }

    @Override
    public void specificRequest() {
        twoWayTarget.request();
    }
}
public class TwoWayAdapterTest {
    public static void main(String[] args) {
        System.out.println("目标通过双向适配器访问适配者：");
        TwoWayAdaptee twoWayAdaptee = new AdapteeRealize();
        TwoWayTarget twoWayTarget = new TwoWayAdapter(twoWayAdaptee);
        twoWayTarget.request();;
        System.out.println("-----------------------");
        System.out.println("适配器通过双向适配器访问目标：");
        twoWayTarget = new TargetRealize();
        twoWayAdaptee = new TwoWayAdapter(twoWayTarget);
        twoWayAdaptee.specificRequest();
    }
}
